<template>
  <div class="app-container">
    <h3>医院设置列表</h3>
    <!-- 表单查询 -->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="医院名称">
        <el-input v-model="searchObj.hosname" placeholder="医院名称" />
      </el-form-item>
      <el-form-item label="医院编码">
        <el-input v-model="searchObj.hoscode" placeholder="医院编码" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getList()">查询</el-button>
      </el-form-item>
    </el-form>

    <div>   
        <el-button type="danger" size="mini" @click="removeDatas()">批量删除</el-button>
    </div>

    <!-- 列表 -->
    <el-table :data="list" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column prop="hosname" label="医院名称" />
      <el-table-column prop="hoscode" label="医院编码" />
      <el-table-column prop="apiUrl" label="api基础路径" width="200" />
      <el-table-column prop="contactsName" label="联系人姓名" />
      <el-table-column prop="contactsPhone" label="联系人手机" />
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? "可用" : "不可用" }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="280" align="center">
         <template slot-scope="scope">
           <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeDataById(scope.row.id)">删除</el-button>
           <el-button type="primary" v-if="scope.row.status==1" size="mini" icon="el-icon-delete" @click="lockData(scope.row.id,0)">锁定</el-button>
           <el-button type="danger" v-if="scope.row.status==0" size="mini" icon="el-icon-delete" @click="lockData(scope.row.id,1)">解锁</el-button>

           <router-link :to="'/hospSet/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-delete">编辑</el-button>
           </router-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination :current-page="current" :page-size="limit" :total="total"
    style="padding: 30px 0; text-align: center"
    layout:="total,prev,pager,next,jumper" @current-change="getList" />
  </div>
</template>

<script>
//引入接口定义js文件
import * as hospSet from "@/api/hospSet";

export default {
  //定义变量和初始值
  data() {
    return {
      current: 1,
      limit: 5,
      searchObj: {},
      list: [],
      total: 0,
      multipleSelection: []
    };
  },
  created() {
    //页面渲染前执行
    this.getList();
  },
  methods: {
    /* 锁定 */
    lockData(id,status){
      hospSet.lockHospSet(id,status)
      .then(response => {
          this.getList(this.current)
        })
    },

    /* 获取复选框id */
    handleSelectionChange(selection){
      this.multipleSelection = selection
    },
    /* 批量删除 */
    removeDatas(){
       this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {

          // 获取ids
          const idList = this.multipleSelection.map(row=>row.id)

           hospSet.batchHospSet(idList)
             .then(response=>{
               this.$message({
                  type: 'success',
                  message: '删除成功!'
              })
              //刷新页面
              this.getList(1)
          })
      })
    },

    /* 展示列表 */
    getList(page = 1) {
      this.current = page;
      hospSet
        .getHospSetList(this.current, this.limit, this.searchObj)
        .then(response => {
          //请求成功
          //返回集合给list
          this.list = response.data.records;
          //总记录
          this.total = response.data.total;
        })
        .catch((error) => {
          //请求失败
          console.log(error)
        });
    },

    removeDataById(id) {
       this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           hospSet.deleteHospSet(id)
             .then(response=>{
               this.$message({
                  type: 'success',
                  message: '删除成功!'
              })
              //刷新页面
              this.getList(1)
          })
      })
    }
  }
}
</script>